package com.xiaoshuidi.cloud.module.member.excel;

import com.alibaba.excel.util.WorkBookUtil;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.handler.WorkbookWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

public class EasyWriteHandler implements WorkbookWriteHandler, SheetWriteHandler, RowWriteHandler, CellWriteHandler {
    private Integer startRowIndex;
    private Integer lastColumnIndex;



    @Override
    public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row,
                                Integer relativeRowIndex, Boolean isHead) {
        if(startRowIndex == null){
            startRowIndex = row.getRowNum();
            lastColumnIndex = Integer.valueOf(row.getLastCellNum());
        }
    }

    public EasyWriteHandler(){}
    public Cell cell(Sheet sheet , int rowIndex, int cellIndex){
        Row row = sheet.getRow(rowIndex);
        if (row == null) {
            row = WorkBookUtil.createRow(sheet, rowIndex);
        }
        Cell cell = row.getCell(cellIndex);
        if (cell == null) {
            cell = WorkBookUtil.createCell(row, cellIndex);
        }
        return cell;
    }

    public Integer getStartRowIndex() {
        return startRowIndex;
    }

    public Integer getLastColumnIndex() {
        return lastColumnIndex;
    }

}
